using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Dal.Helper;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Dal
{    
	/// <summary>
    /// TaskRunLog 
    /// </summary>
    public class TaskRunLogs:ITaskRunLogs
    {

		/// <summary>
		/// 添加TaskRunLog
		/// </summary>
		/// <param name="taskrunlog"></param>
		/// <returns></returns>
		public long Add(TaskRunLog taskrunlog)
		{
			string sql = @"INSERT INTO [TaskRunLog]
			([TaskId],[State],[RunLog],[RuanTime])
			VALUES
			(@TaskId,@State,@RunLog,@RuanTime) 
			SELECT SCOPE_IDENTITY()
			";
			var par = new DynamicParameters();
            par.Add("@TaskId", taskrunlog.TaskId, DbType.Int32);
            par.Add("@State", taskrunlog.State, DbType.Int16);
			par.Add("@RunLog",taskrunlog.RunLog , DbType.String);
			par.Add("@RuanTime",taskrunlog.RuanTime , DbType.DateTime);
			return DapWrapper.InnerQueryScalarSql<long>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 修改TaskRunLog
		/// </summary>
		/// <param name="taskrunlog"></param>
		/// <returns></returns>
		public int Update(TaskRunLog taskrunlog)
		{
			string sql = @"UPDATE  [TaskRunLog] SET 
						State=@State,
						RunLog=@RunLog,
						RuanTime=@RuanTime
 WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", taskrunlog.Id, DbType.Int64);
			par.Add("@State",taskrunlog.State , DbType.Int16);
			par.Add("@RunLog",taskrunlog.RunLog , DbType.String);
			par.Add("@RuanTime",taskrunlog.RuanTime , DbType.DateTime);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据id获取TaskRunLog
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public TaskRunLog Get(long id)
		{
			string sql = "select [Id],[State],[RunLog],[RuanTime] FROM [TaskRunLog] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int64);
			return DapWrapper.InnerQuerySql<TaskRunLog>(DbConfig.EpBaseServiceManagerConnectionString, sql, par).FirstOrDefault();
		}
		/// <summary>
		/// 根据id删除TaskRunLog
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Int32 Delete(long id)
		{
			string sql="DELETE [TaskRunLog] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int64);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据ids删除TaskRunLog多条记录
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		public Int32 Deletes(long[] ids)
		{
			if (ids.Length == 0)
                return 0;
			string sql="DELETE [TaskRunLog] WHERE Id IN ("+string.Join(",",ids)+")";
			return DapWrapper.InnerExecuteText(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
		/// <summary>
       /// 获取TaskRunLog分页列表
       /// </summary>
       /// <param name="pageIndex">页码</param>
       /// <param name="pageSize">每页显示条数</param>
       /// <param name="beginTime">开始时间</param>
       /// <param name="endTime">结束时间</param>
       /// <param name="recordCount">总记录数</param>
       /// <returns>TaskRunLog列表</returns>
       public List<TaskRunLog> GetPageList(int pageIndex, int pageSize, DateTime? beginTime, DateTime? endTime, out int recordCount)
       {
           recordCount = 0;
           var par = new DynamicParameters();
           par.Add("@PageIndex", pageIndex, DbType.Int32);
           par.Add("@PageSize", pageSize, DbType.Int32);
           par.Add("@BeginTime", beginTime, DbType.DateTime);
           par.Add("@EndTime", !endTime.HasValue ? endTime : endTime.Value.AddDays(1).AddMilliseconds(-1), DbType.DateTime);
           par.Add("@TotalCount", recordCount, DbType.Int32, ParameterDirection.Output);
           var result = DapWrapper.InnerQueryProc<TaskRunLog>(DbConfig.EpBaseServiceManagerConnectionString, "proc_GetTaskRunLogPageList", par);
           recordCount = par.Get<int>("@TotalCount");
           return result;
       }
	   /// <summary>
		/// 获取所有TaskRunLog
		/// </summary>
		/// <returns></returns>
		public List<TaskRunLog> GetAll()
		{
			string sql = "select [Id],[State],[RunLog],[RuanTime] FROM [TaskRunLog]";
			
			return DapWrapper.InnerQuerySql<TaskRunLog>(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
    }
}
